from PyQt5 import QtWidgets
from dsw_ui_merge_tool import Ui_Dialog # 导入ui文件转换后的py文件
from PyQt5.QtWidgets import QFileDialog
import PyQt5.QtGui as qg
import pandas as pd
import sys
import dsw_func
import time

class mywindow(QtWidgets.QWidget, Ui_Dialog):
    def  __init__ (self):
        super(mywindow, self).__init__()
        self.setupUi(self)
        self.setWindowTitle(' 烧录文件打包工具 V1.0  by zhubin ')
        self.setWindowIcon(qg.QIcon('dsw.ico'))
        self.pushButton_old_flash.clicked.connect(self.read_old_flash)
        self.pushButton_upgrade.clicked.connect(self.read_upgrade_file)
        self.pushButton_merge.clicked.connect(self.merge)
        self.treeWidget.expandAll()
        self.dev_type = -1

    def read_old_flash(self):
        #选取文件
        filename, filetype =QFileDialog.getOpenFileName(self, "选取文件", ".", "bin Files(*.bin);;All Files(*)")
        if len(filename)==0:
            return
        self.lineEdit_old_flash.setText(filename)
        self.textEdit.setText('load:'+filename)

    def read_upgrade_file(self):
        #选取文件
        filename, filetype =QFileDialog.getOpenFileName(self, "选取文件", ".", "Img Files(*.img);;All Files(*)")
        if len(filename)==0:
            return
        self.lineEdit_upgrade.setText(filename)
        self.textEdit.append('load:' + filename)
        try:
            result = dsw_func.dsw_prase_upgrade_file_head(filename)
        except:
            self.textEdit.append('check header: Faile!')
            return

        time_array = time.localtime(result[1])
        soft_ver = time.strftime("%Y-%m-%d %H:%M:%S", time_array)
        print(result[0],soft_ver)
        find=False
        for key in dsw_func.dsw_dev_type_table.keys():
            if key==result[0]:
                find = True
                self.dev_type = result[0]
        if find:
            self.treeWidget.topLevelItem(0).setText(1, dsw_func.dsw_dev_type_table[result[0]][0])
            self.textEdit.append('check header: Ok!')
        else:
            string='check header: Faile! not support devtype: %d'%result[0]
            self.textEdit.append(string)
            return

        self.treeWidget.topLevelItem(1).setText(1, soft_ver)
        self.treeWidget.topLevelItem(2).child(0).setText(1, str(result[2], 'utf-8').strip('\0'))
        self.treeWidget.topLevelItem(2).child(1).setText(1, str(result[3], 'utf-8').strip('\0'))
        self.treeWidget.topLevelItem(2).child(2).setText(1, str(result[4], 'utf-8').strip('\0'))
        self.treeWidget.topLevelItem(2).child(3).setText(1, str(result[5], 'utf-8').strip('\0'))

    # 进行处理
    def merge(self):
        try:
            if self.dev_type == -1:
                return
            old_flash_file = self.lineEdit_old_flash.text()
            new_upgrade_file = self.lineEdit_upgrade.text()
            # dsw_func.dsw_create_mtd_app_bin(new_upgrade_file)
            # self.textEdit.append('creat mtd_app.bin')
            dsw_func.update_old_flash(self.dev_type,old_flash_file,new_upgrade_file,'flash.bin')
            fail_result = r'生成烧录文件:flash.bin'
            self.textEdit.append(fail_result)
        except:
            fail_result = r'打包失败！'
            self.textEdit.append(fail_result)

def main():
    app = QtWidgets.QApplication(sys.argv)
    ui = mywindow()
    ui.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

# 打包命令
# Pyinstaller -F -w -i dsw.ico main.py
